package integracion.Cliente;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import negocio.Cliente.TransferCliente;
import negocio.Cliente.TransferEmpresa;
import negocio.Cliente.TransferParticular;

public class DAOClientesImp implements DAOCliente{
	private int lastId;
	
	public DAOClientesImp() 
	{ 
	  try
	  {
		this.lastId=-1;
		File archivo=new File("C:/cardealer/Clientes/index.txt");
		if(archivo.exists())
		 	{
			 BufferedReader bfr = new BufferedReader(new FileReader(archivo));
			 this.lastId=Integer.parseInt(bfr.readLine());		
			 bfr.close();
		 	}
		 else
		 	{
			File directorio = new File("c:/cardealer/Clientes/");
			directorio.mkdirs();
		
			PrintWriter pw = new PrintWriter( new FileWriter((archivo)));			
			pw.println(this.lastId);
			pw.close();
		 	}			
	  }catch(IOException e)
	  {
		 e.printStackTrace();
	  }
	}
	
	public boolean create(TransferEmpresa transfer)
	{		
		int id = idForParams(transfer.getDNI()+" "+
						   transfer.getDomicilioSocial()+" "+
						   transfer.getCodigoCNAE());
		Boolean resultado=false;
		try
		{
		if(id>-1)
			{
			 File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(id) + ".txt");				
			 PrintWriter pw = new PrintWriter( new FileWriter((archivo)),true);	
			 pw.println("true");
			 pw.println("E");
			 pw.println(transfer.getDNI());
			 pw.println(transfer.getDomicilioSocial());
			 pw.println(transfer.getCodigoCNAE());
			 pw.close();				
			resultado = true;
			}
		else
			{
			 lastId++;
			 File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(lastId) + ".txt");
			 File index = new File ("c:/cardealer/Clientes/index.txt");
		
    		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));
			 PrintWriter pwindex = new PrintWriter( new FileWriter((index)));
			
			 pw.println("true");
			 pw.println("E");
			 pw.println(transfer.getDNI());
			 pw.println(transfer.getDomicilioSocial());
			 pw.println(transfer.getCodigoCNAE());							
			 pwindex.println(lastId);	
			 pw.close();
			 pwindex.close();			
			 resultado = true;
			}
		}
		catch(IOException e)
		{
		 e.printStackTrace();
		}
		return resultado;
	}		
		   
	public boolean update(TransferEmpresa transfer)  {
		
		File archivo=new File("C:/cardealer/Clientes/"+transfer.getId()+".txt");				
			try
			  {			
				PrintWriter pw = new PrintWriter( new FileWriter((archivo)));					
				pw.println("true");
				pw.println("E");
				pw.println(transfer.getDNI());
				pw.println(transfer.getDomicilioSocial());
				pw.println(transfer.getCodigoCNAE());
				pw.close();	
				return true;								
			  }catch(Exception e){
			    	e.printStackTrace();
			     }	
			return false;
		}
	
	public boolean creable(TransferEmpresa transfer)
	{
	 int id = idForParams(transfer.getDNI()+" "+
				   		  transfer.getDomicilioSocial()+" "+
				   		  transfer.getCodigoCNAE());
	 int id2 = idForDNI(transfer.getDNI());
	 if (id<0 && id2<0) return true;
	 if(id2>=0)return false;
	 if(!checkFlag(id)) return true;
	 return false;
	}
	
	public boolean deletable(TransferCliente transfer)
	{
	 if(transfer.getId()<0 || transfer.getId()>lastId) return false;
	 if(!checkFlag(transfer.getId()))return false;
	 return true;
	}
	
	public boolean updatable(TransferEmpresa transfer)
	{
		if(transfer.getId()<0 || transfer.getId()>lastId) return false;
		if(checkFlag(transfer.getId())) return true;
		return false;
	}
		
	public boolean create(TransferParticular transfer)
	{		
		int id = idForParams(transfer.getDNI()+" "+
						   	transfer.getName());
		Boolean resultado=false;
		try
		{
		if(id>-1)
			{
			 File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(id) + ".txt");				
			 PrintWriter pw = new PrintWriter( new FileWriter((archivo)),true);	
			 pw.println("true");
			 pw.println("P");
			 pw.println(transfer.getDNI());
			 pw.println(transfer.getName());
			 pw.close();				
			resultado = true;
			}
		else
			{
			 lastId++;
			 File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(lastId) + ".txt");
			 File index = new File ("c:/cardealer/Clientes/index.txt");
		
    		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));
			 PrintWriter pwindex = new PrintWriter( new FileWriter((index)));
			
			 pw.println("true");
			 pw.println("P");
			 pw.println(transfer.getDNI());
			 pw.println(transfer.getName());							
			 pwindex.println(lastId);	
			 pw.close();
			 pwindex.close();			
			 resultado = true;
			}
		}
		catch(IOException e)
		{
		 e.printStackTrace();
		}
		return resultado;
	}		
	
	public boolean delete(TransferCliente transfer){	
		File archivo = new File("c:/cardealer/Clientes/"+transfer.getId()+".txt");
		try
		{				
		 ArrayList<String> lineas = new ArrayList<String>();
		 
		 BufferedReader bfr = new BufferedReader(new FileReader(archivo));
		 bfr.readLine();
		 String leido = bfr.readLine();
		 while(leido!=null)
		 {
			 lineas.add(leido);
			 leido=bfr.readLine();
		 }
		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));	
		 pw.println("false");
		 for(int i=0;i<lineas.size();i++)
		 {
			 pw.println(lineas.get(i));	 
		 }
		 bfr.close();
		 pw.close();
		 return true;		 
		}catch(Exception e){
			e.printStackTrace();
		 }
	   return false; 
	}
		
	public boolean update(TransferParticular transfer)  {
		
		File archivo=new File("C:/cardealer/Clientes/"+transfer.getId()+".txt");				
			try
			  {			
				PrintWriter pw = new PrintWriter( new FileWriter((archivo)));					
				pw.println("true");
				pw.println("P");
				pw.println(transfer.getDNI());
				pw.println(transfer.getName());				
				pw.close();	
				return true;								
			  }catch(Exception e){
			    	e.printStackTrace();
			     }	
			return false;
		}
		
	public boolean creable(TransferParticular transfer)
	{
	 int id = idForParams(transfer.getDNI()+" "+ transfer.getName());
	 int id2 = idForDNI(transfer.getDNI());
	 if (id<0 && id2<0) return true;
	 if(id2>=0)return false;
	 if(!checkFlag(id)) return true;
	 return false;
	}
		
	public boolean updatable(TransferParticular transfer)
	{
		if(transfer.getId()<0 || transfer.getId()>lastId) return false;
		if(checkFlag(transfer.getId())) return true;
		return false;
	}
		
	public TransferParticular buscarParticular(int id)
	{
		File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(id) + ".txt");
		TransferParticular transfer=null;
		try
		{	
    		BufferedReader bfr = new BufferedReader(new FileReader(archivo));	
			bfr.readLine();
			if("P".equalsIgnoreCase(bfr.readLine()))
			 {
			  transfer = new TransferParticular(id,
			  bfr.readLine(),
			  bfr.readLine());
			 }			
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		return transfer;
	}
	
	public TransferEmpresa buscarEmpresa(int id)
	{
		File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(id) + ".txt");
		TransferEmpresa transfer=null;
		try
		{	
    		BufferedReader bfr = new BufferedReader(new FileReader(archivo));	
			bfr.readLine();
			if("E".equalsIgnoreCase(bfr.readLine()))
			 {
				transfer = new TransferEmpresa(id,
 						 bfr.readLine(),
 						 bfr.readLine(),
 						 Integer.parseInt(bfr.readLine()));
			 }			
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		return transfer;
	}
	
	public boolean searchable(int id)
	{
	 if(id<0 || id>lastId)return false;
	 if(!checkFlag(id)) return false;
	 return true;
	}
	
	private int idForParams(String params) 
	{	
	 try
	   {	
	  for (int i=0;i<=this.lastId;i++)
		 {
		   File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(i) + ".txt");
		   BufferedReader bfr = new BufferedReader(new FileReader(archivo));
		   bfr.readLine();
		   if("E".equalsIgnoreCase(bfr.readLine()))
		     {	   
			  if(params.equalsIgnoreCase(bfr.readLine()+" "+
				    				  	 bfr.readLine()+" "+
				    				  	 bfr.readLine()))
			   return i;		      
		     }
	       else {
	    	   if(params.equalsIgnoreCase(bfr.readLine()+ " "+ bfr.readLine()))
	    	   		return i;	
	  	        }
	  	 }
		}catch(IOException e)
		    {
		     e.printStackTrace();
		    }	  	
	 return -1;
	}
	
	private int idForDNI(String DNI)
	{	
		 try
		   {	
		  for (int i=0;i<=this.lastId;i++)
			 {
			   File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(i) + ".txt");
			   BufferedReader bfr = new BufferedReader(new FileReader(archivo));
			   bfr.readLine();
			   bfr.readLine();			   
			   if(DNI.equalsIgnoreCase(bfr.readLine()))
				   return i;
		  	 }
			}catch(IOException e)
			    {
			     e.printStackTrace();
			    }	  	
		 return -1;
		}
	
    private boolean checkFlag (int id)
	{		
	   File archivo= new File("c:/cardealer/Clientes/" + Integer.toString(id) + ".txt");
		{
		  try{
			  BufferedReader bfr = new BufferedReader(new FileReader(archivo));	 
			  return Boolean.parseBoolean(bfr.readLine());
		  	 }catch(Exception e)
		  	 {e.printStackTrace();			
		  	 }
		 } 	 
		return false;
	}

}
